package com.heima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.heima.reggie.bean.ShoppingCart;
import com.heima.reggie.mapper.ShoppingCartMapper;
import com.heima.reggie.service.ShoppingCartServcie;
import com.heima.reggie.utils.BaseContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class ShoppingCartServiceImpl implements ShoppingCartServcie {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Override
    public ShoppingCart add(ShoppingCart shoppingCart,long id) {
        System.out.println("-------");
        System.out.println(shoppingCart);
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, id);
        lqw.eq(shoppingCart.getDishId() != null, ShoppingCart::getDishId, shoppingCart.getDishId());
        lqw.eq(shoppingCart.getSetmealId() != null, ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectOne(lqw);
        if (shoppingCart1 == null) {
            shoppingCart.setUserId(id);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            System.out.println("-------");
            System.out.println(shoppingCart);
            shoppingCartMapper.insert(shoppingCart);
            return shoppingCart;
        } else {
            shoppingCart1.setNumber(shoppingCart1.getNumber() + 1);
            shoppingCartMapper.updateById(shoppingCart1);
            return shoppingCart1;
        }
    }

    @Override
    public List<ShoppingCart> findByUid(long uid) {
       return  shoppingCartMapper.selectList(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,uid));
    }

    @Override
    public int clean(long currentId) {
        return shoppingCartMapper.delete(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,currentId));
    }

    @Override
    public void deleteBySid(Long id) {
        shoppingCartMapper.deleteById(id);
    }
}
